package com.foodorder.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 商品实体类
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "products")
@EntityListeners(AuditingEntityListener.class)
public class Product {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 店铺ID
     */
    @Column(name = "store_id", nullable = false)
    private Long storeId;
    
    /**
     * 分类ID
     */
    @Column(name = "category_id")
    private Long categoryId;
    
    /**
     * 商品名称
     */
    @Column(nullable = false, length = 100)
    private String name;
    
    /**
     * 商品描述
     */
    @Column(columnDefinition = "TEXT")
    private String description;
    
    /**
     * 商品价格
     */
    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal price;
    
    /**
     * 商品图片URL
     */
    @Column(length = 255)
    private String image;
    
    /**
     * 库存数量
     */
    @Column(nullable = false)
    private Integer stock = 0;
    
    /**
     * 月销量
     */
    @Column(name = "monthly_sales", nullable = false)
    private Integer monthlySales = 0;
    
    /**
     * 商品规格（JSON格式存储）
     */
    @Column(length = 1000)
    private String specifications;
    
    /**
     * 商品状态
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private ProductStatus status = ProductStatus.AVAILABLE;
    
    /**
     * 是否推荐
     */
    @Column(nullable = false)
    private Boolean recommended = false;
    
    /**
     * 排序权重
     */
    @Column(nullable = false)
    private Integer sortOrder = 0;
    
    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    /**
     * 乐观锁版本号
     */
    @Version
    private Integer version = 0;
    
    /**
     * 销量统计
     */
    @Column(nullable = false)
    private Integer sales = 0;

    // 显式添加getter和setter方法以确保编译正确
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getStoreId() {
        return storeId;
    }

    public void setStoreId(Long storeId) {
        this.storeId = storeId;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Integer getMonthlySales() {
        return monthlySales;
    }

    public void setMonthlySales(Integer monthlySales) {
        this.monthlySales = monthlySales;
    }

    public ProductStatus getStatus() {
        return status;
    }

    public void setStatus(ProductStatus status) {
        this.status = status;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }
    
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Integer getSales() {
        return sales;
    }

    public void setSales(Integer sales) {
        this.sales = sales;
    }

    public Boolean getRecommended() {
        return recommended;
    }

    public void setRecommended(Boolean recommended) {
        this.recommended = recommended;
    }

    public Boolean isRecommended() {
        return recommended;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    /**
     * 商品状态枚举
     */
    public enum ProductStatus {
        AVAILABLE,    // 可售
        UNAVAILABLE,  // 不可售
        SOLD_OUT,     // 售罄
        OFFLINE       // 下架
    }
} 